package dao

import (
	"cld-quick-cli/database/models"
	"cld-quick-cli/setup"
	"errors"
	"go.uber.org/zap"
)

var (
	// 缓存，一些不怎么变的数据缓存到内存中！
	cl []models.CommunityListModel
)

// GetCommunityList 获取社区列表
func GetCommunityList() ([]models.CommunityListModel, error) {
	if cl != nil {
		return cl, nil
	}
	sqlStr := "select community_id,community_name from community"
	err := setup.MysqlDB.Select(&cl, sqlStr)
	if err != nil {
		zap.L().Error(err.Error())
		return nil, errors.New("社区列表获取失败！")
	}
	return cl, nil
}

// GetCommunityDetailsByCommunityId 获取社区详情
func GetCommunityDetailsByCommunityId(id *string) (*models.CommunityModel, error) {
	var community models.CommunityModel
	sqlStr := "select " +
		"community_id,community_name,introduction,create_time,update_time " +
		"from community where community_id = ?"
	err := setup.MysqlDB.Get(&community, sqlStr, id)
	if err != nil {
		zap.L().Error(err.Error())
		return nil, errors.New("查询社区详情失败！")
	}
	return &community, nil
}
